package Util::TuanZhuanYi::TuanZhuanYi;
use Moose;
use namespace::autoclean;
use JSON;
use Time::Piece;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

#####################################################
#Input: 
#	user_id	
#Return: 
#	requests		
#####################################################
sub find {
	my ($self, $tzy_id) = @_;

	return  $self->schema->resultset('TuanZhuanYiComplex')->search({}, 
	{
		bind => [0, 0, $tzy_id]	
	}
	)->first();
}

sub find_zhuanyis_for_user_count { my ($self, $user_id) = @_;
	return  $self->schema->resultset('TuanZhuanYiComplex')->search({}, 
	{
		bind => [$user_id, $user_id, 0]	
	}
	)->count();

}

sub find_by_shangyou_tuan_id_requester_id {
	my ($self, $shangyou_tuan_id,$requester_id,$target_univ_id) = @_;

	return  $self->schema->resultset('TuanZhuanYi')->search({'shangyou_tuan_id' => $shangyou_tuan_id, 'requester_id' => $requester_id, 'zhuanyi_univ_id' => $target_univ_id},{})->first();
}


sub find_xiayou_tuan_id {
	my ($self, $tuan_id) = @_;

	my @result;
	my @arr = $self->schema->resultset('TuanZhuanYi')->search({'shangyou_tuan_id' => $tuan_id}, {columns => [qw/xiayou_tuan_id/]});
	foreach my $arr1(@arr) {
		push (@result, $arr1->get_column("xiayou_tuan_id"));
	}
	return @result;
}

sub find_shangyou_tuan_id {
	my ($self, $tuan_id) = @_;

	my $std = $self->schema->resultset('TuanZhuanYi')->search({'xiayou_tuan_id' => $tuan_id}, {columns => [qw/shangyou_tuan_id/]})->first();
	my $result = '';
	if (defined $std) {
		$result = $std->get_column("shangyou_tuan_id");	
	}
	return $result;
}

sub find_tzy_id {
	my ($self, $shangyou_tuan_id, $xiayou_tuan_id) = @_;

	my $std = $self->schema->resultset('TuanZhuanYi')->search({'xiayou_tuan_id' => $xiayou_tuan_id},{'shangyou_tuan_id' => $shangyou_tuan_id}, {columns => [qw/id/]})->first();
	my $result = '';
	if (defined $std) {
		$result = $std->get_column("id");	
	}
	return $result;
}

sub zhuanyi_request_search {
	my ($self, $user_id, $page) = @_;

	return  $self->schema->resultset('TuanZhuanYiComplex')->search({}, 
	{
		bind => [$user_id, $user_id, 0],
		order_by => {-desc=>"created"},
		page => $page
	}
	);

}

sub update_status {
	my ($self, $tzy_id, $status, $user_id) = @_;
	my $request = $self->schema->resultset('TuanZhuanYi')->search({id => $tzy_id }, {})->first();
	$request->update({
		status => $status,
		updated_by => $user_id,
	});
	return  $self->schema->resultset('TuanZhuanYiComplex')->search({}, 
	{
		bind => [0, 0, $tzy_id]	
	})->first();
}

sub update_xiayou_tuan {
	my ($self, $tzy_id, $tuan_id, $user_id) = @_;
	my $request = $self->schema->resultset('TuanZhuanYi')->search({id => $tzy_id }, {})->first();
	$request->update({
		xiayou_tuan_id => $tuan_id,
		updated_by => $user_id,
	});
	return  $self->schema->resultset('TuanZhuanYiComplex')->search({}, 
	{
		bind => [0, 0, $tzy_id]	
	})->first();
}

sub request_tuan_zhuanyi {
	my ($self, $shangyou_user_id, $xiayou_user_id, $shangyou_tuan_id, $zhuanyi_univ_id, $xiayou_percentage, $req_detail, $status, $req_user_id) = @_;

	my $request = $self->schema->resultset('TuanZhuanYi')->create({
			shangyou_user_id => $shangyou_user_id,
			xiayou_user_id => $xiayou_user_id,
			shangyou_tuan_id => $shangyou_tuan_id,
			zhuanyi_univ_id => $zhuanyi_univ_id,
			xiayou_percentage => $xiayou_percentage,
			requester_id => $req_user_id,
			req_detail => $req_detail,
			status => $status,
			created_by => $req_user_id,
			updated_by => $req_user_id
			});
	return $request;
}


__PACKAGE__->meta->make_immutable;

1;
